package com.ado.abyss.core.manager.impl;

import com.ado.abyss.core.dal.dao.SysMenuMapper;
import com.ado.abyss.core.dal.dao.SysRoleMapper;
import com.ado.abyss.core.dal.dao.SysUserMapper;
import com.ado.abyss.core.dal.dao.SysUserRoleMapper;
import com.ado.abyss.core.dal.model.SysMenu;
import com.ado.abyss.core.dal.model.SysRole;
import com.ado.abyss.core.dal.model.SysUser;
import com.ado.abyss.core.dal.model.SysUserRole;
import com.ado.abyss.core.manager.SysUserManager;
import com.ado.abyss.core.params.page.ColumnFilter;
import com.ado.abyss.core.params.page.MybatisPageHelper;
import com.ado.abyss.core.params.page.PageRequest;
import com.ado.abyss.core.params.page.PageResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author adonais
 * @date 2020/7/9
 * @description SysUserManagerImpl
 **/
@Service(value = "sysUserManager")
public class SysUserManagerImpl extends ServiceImpl<SysUserMapper , SysUser> implements SysUserManager {

    @Autowired
    private SysUserMapper sysUserMapper ;
    @Autowired
    private SysMenuMapper sysMenuMapper ;
    @Autowired
    private SysRoleMapper sysRoleMapper ;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper ;

    @Override
    public SysUser findByName(String username) {
        SysUser sysUser = sysUserMapper.findByName(username) ;
        return sysUser;
    }

    @Override
    public PageInfo<SysUser> findPage(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum() , pageRequest.getPageSize()) ;
        List<SysUser> sysUsers = sysUserMapper.findAll() ;
        // 加载用户角色信息
        findUserRoles(sysUsers);
        return new PageInfo<SysUser>(sysUsers);
    }

    @Override
    public SysUser getByUserId(Long id) {
        return sysUserMapper.getByUserId(id);
    }

    @Override
    public List<SysUser> findAll() {
        return sysUserMapper.findAll();
    }

    @Override
    @Transactional
    public boolean deleteByIds(List<Long> ids) {
        boolean flag = false ;
        int result = sysUserMapper.deleteByIds(ids) ;
        if(result == 1) {
            flag =  true ;
        }
        return flag;
    }

    @Override
    @Transactional
    public boolean saveUser(SysUser record) {
        boolean flag = false ;
        int result = sysUserMapper.insertSelective(record) ;
        if (result == 1) {
            flag = true ;
        }
        return flag;
    }

    /**
     * 加载用户角色
     * @param
     */
    private void findUserRoles(List<SysUser> sysUsers) {
        for(SysUser sysUser:sysUsers) {
            List<SysUserRole> userRoles = findUserRoles(sysUser.getId());
            sysUser.setUserRoles(userRoles);
            sysUser.setRoleNames(getRoleNames(userRoles));
        }
    }

    private String getRoleNames(List<SysUserRole> userRoles) {
        StringBuilder sb = new StringBuilder();
        for(Iterator<SysUserRole> iter = userRoles.iterator(); iter.hasNext();) {
            SysUserRole userRole = iter.next();
            SysRole sysRole = sysRoleMapper.selectByPrimaryKey(userRole.getRoleId());
            if(sysRole == null) {
                continue ;
            }
            sb.append(sysRole.getRemark());
            if(iter.hasNext()) {
                sb.append(", ");
            }
        }
        return sb.toString();
    }

    @Override
    public Set<String> findPermissions(String userName) {
        Set<String> perms = new HashSet<>();
        List<SysMenu> sysMenus = sysMenuMapper.findByUserName(userName);
        for(SysMenu sysMenu:sysMenus) {
            if(sysMenu.getPerms() != null && !"".equals(sysMenu.getPerms())) {
                perms.add(sysMenu.getPerms());
            }
        }
        return perms;
    }

    @Override
    public List<SysUserRole> findUserRoles(Long userId) {
        return sysUserRoleMapper.findUserRoles(userId);
    }
}
